package xyz.zhzby.cyzx.security.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import xyz.zhzby.cyzx.servicebase.domain.DataLogInfo;
import xyz.zhzby.cyzx.servicebase.utils.DateUtils;
import xyz.zhzby.cyzx.servicebase.utils.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * @author zby
 * @date 2021/12/07
 */
@Data
@ApiModel("用户实体类")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String nickName;

    private String name;

    private String password;

    private Integer gender;

    private String phone;

    private String email;

    private String avatar;

    private String signature;


    @ApiOperation("填充create信息")
    public void fillCreateInfo(DataLogInfo dataLogInfo, String userField) {
        Date now = DateUtils.getCurrDate();
        String user = obtainUser(userField);
        dataLogInfo.setCreateTime(now);
        dataLogInfo.setCreateUser(user);
        dataLogInfo.setUpdateTime(now);
        dataLogInfo.setUpdateUser(user);
    }

    @ApiOperation("填充update信息")
    public void fillUpdateInfo(DataLogInfo dataLogInfo, String userField) {
        Date now = DateUtils.getCurrDate();
        String user = obtainUser(userField);
        dataLogInfo.setUpdateTime(now);
        dataLogInfo.setUpdateUser(user);
    }

    @ApiOperation(value = "根据类型获取要填充的用户信息", tags = {"userField 要填充的用户字段类型"})
    private String obtainUser(String userField) {
        if (DataLogInfo.USER_FIELD_PHONE.equals(userField)) {
            String phone = getPhone();
            if (StringUtils.isNotNullOrBlank(phone)) {
                return phone;
            }
        } else if (DataLogInfo.USER_FIELD_USER_ID.equals(userField)) {
            return String.valueOf(getId());
        } else if (DataLogInfo.USER_FIELD_USERNAME.equals(userField)) {
            return getUsername();
        } else if (DataLogInfo.USER_FIELD_ACTUAL_NAME.equals(userField)) {
            String name = getName();
            if (StringUtils.isNotNullOrBlank(name)) {
                return name;
            }
        }
        return String.valueOf(getId());
    }

}
